import 'package:xvideos/hj_utils/api_service/vid_service.dart';
import 'package:xvideos/hj_utils/text_util.dart';
import 'package:xvideos/tools_base/debug_log.dart';
import '../hj_model/video_model.dart';
import '../tools_base/global_store/store.dart';
import '../tools_base/net/net_manager.dart';
import 'date_time_util.dart';

///是否需要购买VIP
bool needBuyVip(VideoModel? videoModel) {
  if (videoModel?.freeArea == true) {
    return false;
  }

  ///VIP判断
  if (TextUtil.isNotEmpty(globalStore.getMe()?.vipExpireDate)) {
    DateTime? dateTime = DateTime.tryParse(globalStore.getMe()?.vipExpireDate ?? "");
    if (dateTime?.isAfter((netManager.getFixedCurTime())) == true) {
      return false;
    }
  }

  ///是否需要金币
  if (videoModel?.isCoinVideo() == true) {
    return false;
  }

  ///自己
  if (globalStore.isMe(videoModel?.publisher?.uid)) {
    return false;
  }

  return true;
}

/*
会员权益：
普通会员： VIP长视频  VIP抖音 社区帖子VIP 图集VIP 小说VIP ACGVIP
高级会员：VIP+金币视频（长视频，抖音），社区VIP帖子。图集金币 小说 金币 ACG 。 暗网视频和社区金币帖子除外
超级会员： 全网通 包含暗网+社区金币帖子
黄游单独购买
* */
bool needBuyVideo(VideoModel? model) {
  if (null == model) return false;
  if ((model.originCoins ?? 0) == 0) return false;
  if (globalStore.isMe(model.publisher?.uid)) {
    return false;
  }
  if (model.freeArea == true) {
    return false;
  }
  if (globalStore.isVIP() && model.coins == 0) {
    return false;
  }
  if (globalStore.isVIPTwoLevel()) {
    return false;
  }
  if (globalStore.isAWVIP()) {
    return false;
  }
  // UserInfoModel? meInfo = globalStore.getMe();
  // if (model.videoType != 1) {
  //   // 动漫不走vip金币免逻辑
  //   if (meInfo?.allGoldVideoFree == true && DateTimeUtil.calTime3(meInfo?.goldVideoFreeExpire) > 0) {
  //     return false;
  //   } else if (DateTimeUtil.calTime3(meInfo?.goldVideoFreeExpire) > 0 && (model.coins ?? 50) < 50) {
  //     return false;
  //   }
  // }
  if (globalStore.isVIP() && DateTimeUtil.calTime3(globalStore.getMe()?.goldVideoFreeExpire) > 0 && (model.originCoins ?? 50) <= 50) {
    return false;
  }
  return !(model.vidStatus?.hasPaid ?? false);
}

bool needBuyPostVideo(VideoModel? model) {
  if (null == model) return false;

  if (model.freeArea == true) {
    return false;
  }

  if (globalStore.isAWVIP()) return false;
  bool isCoin = !(model.freeArea ?? false) && (model.originCoins != 0);

  if (globalStore.isVIP() && !isCoin) return false;
  if (globalStore.isVIP() && DateTimeUtil.calTime3(globalStore.getMe()?.goldVideoFreeExpire) > 0 && (model.originCoins ?? 50) <= 50) {
    return false;
  }
  if (globalStore.isVIPTwoLevel() && !isCoin) return false;
  return !(model.vidStatus?.hasPaid ?? false);
}

///发送播放记录

sendRecord(Duration? position, Duration? duration, VideoModel? videoModel) async {
  if (position == null || duration == null) {
    return;
  }
  if (position.inMilliseconds < 1000) {
    return;
  }
  int durationInMil = duration.inMilliseconds;
  if (durationInMil <= 0) {
    durationInMil = (videoModel?.playTime ?? 0) * 1000;
  }
  // Map<String, dynamic> param = {};
  // param["videoID"] = videoModel.id;
  // param["playWay"] = videoModel.coins == 0 ? 0 : 1;
  // param["longer"] = position.inSeconds;
  // param["progress"] =
  //     ((position.inMilliseconds / duration.inMilliseconds) * 100).toInt();
  // param["via"] = 1;
  // if (videoModel.tags?.isNotEmpty ?? false) {
  //   param["tagID"] = videoModel.tags[0].id;
  // }
  try {
    if (videoModel?.videoType == 1) return; // 动漫不调用这个
    String? videoID = videoModel?.id;
    int playWay = videoModel?.coins == 0 ? 0 : 1;
    int longer = position.inSeconds;
    int progress = ((position.inMilliseconds / durationInMil) * 100).toInt();
    int via = 1;
    String? tagID;
    if (videoModel?.tags?.isNotEmpty ?? false) {
      tagID = videoModel?.tags?.firstOrNull?.id;
    }
    await VidService.sendVideoRecord(videoID ?? '',
        playWay: playWay, longer: longer, progress: progress, via: via, tagID: tagID ?? '');
  } catch (e) {
    debugLog('sendVideoRecord', "error:$e");
  }
  // sendVideoRecord(param);
}

double resolutionWidth(String? resolution) {
  if (TextUtil.isNotEmpty(resolution)) {
    var arr = resolution?.split("*") ?? [];
    if (arr.isNotEmpty) {
      return double.parse(arr[0]);
    }
  }
  return .0;
}

double resolutionHeight(String? resolution) {
  if (TextUtil.isNotEmpty(resolution)) {
    var arr = resolution?.split("*") ?? [];
    if (arr.isNotEmpty && arr.length > 1) {
      return double.parse(arr[1]);
    }
  }
  return .0;
}

class VideoResolution {
  double videoWidth;
  double videoHeight;

  VideoResolution(this.videoWidth, this.videoHeight);

  @override
  String toString() {
    return "videoWidth:$videoWidth  videoHeight:$videoHeight";
  }
}

VideoResolution configVideoSize(
    double containerWidth, double containerHeight, double? resolutionWidth, double? resolutionHeight,
    [bool enableLoose = true]) {
  /// 输出视频宽和高
  double outVideoWidth, outVideoHeight;
  resolutionHeight ??= 1;
  resolutionWidth ??= 1;
  resolutionWidth = (resolutionWidth) <= 0 ? containerWidth : resolutionWidth;
  resolutionHeight = (resolutionHeight) <= 0 ? containerHeight : resolutionHeight;

  /// 视频容器宽高比
  double containerAspectRation = containerWidth / containerHeight;

  /// 视频宽高比
  double videoAspectRation = resolutionWidth / resolutionHeight;
  bool shouldBeWider = videoAspectRation > containerAspectRation;
  if (shouldBeWider) {
    //尝试按屏幕宽计算视频高
    outVideoWidth = containerWidth;
    outVideoHeight = containerWidth / videoAspectRation;
    if (enableLoose && outVideoHeight / containerHeight > 0.7) {
      //定宽算出来的高度 超过0.7,这时按高缩放,裁剪宽
      outVideoHeight = containerHeight;
      outVideoWidth = videoAspectRation * outVideoHeight;
    }
  } else {
    outVideoHeight = containerHeight;
    outVideoWidth = videoAspectRation * outVideoHeight;
    if (enableLoose && outVideoWidth / containerWidth > 0.7) {
      //定高算出来ls的宽度 占屏幕宽比超过0.7,这时按宽缩放,裁剪高
      outVideoWidth = containerWidth;
      outVideoHeight = outVideoWidth / videoAspectRation;
    }
  }
  var vr = VideoResolution(outVideoWidth, outVideoHeight);
  return vr;
}
